home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / af_depthcue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.6 KB  |  164 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1997. */
  3.  
  4. /* A version of the OpenGL Programming Guide's depthcue.c that
  5.    uses the "addfog" routines to add depth cueing as a post-rendering
  6.    pass.  This is not a rather poor application for "addfog" but it
  7.    does demonstrate how it use "addfog".  See "addfog.c" for details.  */
  8.  
  9. /*
  10.  * (c) Copyright 1993, Silicon Graphics, Inc.
  11.  * ALL RIGHTS RESERVED 
  12.  * Permission to use, copy, modify, and distribute this software for 
  13.  * any purpose and without fee is hereby granted, provided that the above
  14.  * copyright notice appear in all copies and that both the copyright notice
  15.  * and this permission notice appear in supporting documentation, and that 
  16.  * the name of Silicon Graphics, Inc. not be used in advertising
  17.  * or publicity pertaining to distribution of the software without specific,
  18.  * written prior permission. 
  19.  *
  20.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  21.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  22.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  23.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  24.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  25.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  26.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  27.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  28.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  29.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  30.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  31.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  32.  * 
  33.  * US Government Users Restricted Rights 
  34.  * Use, duplication, or disclosure by the Government is subject to
  35.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  36.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  37.  * clause at DFARS 252.227-7013 and/or in similar or successor
  38.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  39.  * Unpublished-- rights reserved under the copyright laws of the
  40.  * United States.  Contractor/manufacturer is Silicon Graphics,
  41.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  42.  *
  43.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  44.  */
  45. /*
  46.  *  depthcue.c
  47.  *  This program draws a wireframe model, which uses 
  48.  *  intensity (brightness) to give clues to distance.
  49.  *  Fog is used to achieve this effect.
  50.  */
  51. #include <stdlib.h>
  52. #include <GL/glut.h>
  53. #include "addfog.h"
  54.  
  55. /*  Initialize linear fog for depth cueing.
  56.  */
  57. void myinit(void)
  58. {
  59.     GLfloat fogColor[4] = {0.0, 0.0, 0.0, 1.0};
  60.  
  61.     glFogi (GL_FOG_MODE, GL_LINEAR);
  62.     glHint (GL_FOG_HINT, GL_NICEST);  /*  per pixel   */
  63.     glFogf (GL_FOG_START, 3.0);
  64.     glFogf (GL_FOG_END, 5.0);
  65.     glFogfv (GL_FOG_COLOR, fogColor);
  66.     glClearColor(0.0, 0.0, 0.0, 1.0);
  67.  
  68.     glDepthFunc(GL_LESS);
  69.     glEnable(GL_DEPTH_TEST);
  70.     glShadeModel(GL_FLAT);
  71.  
  72.     afEyeNearFar(3.0, 5.0);
  73.     afFogStartEnd(3.0, 5.0);
  74.     afFogMode(GL_LINEAR);
  75.     afFogColor(0.0, 0.0, 0.0);
  76. }
  77.  
  78. int width, height;
  79. int twopass = 1;
  80.  
  81. /*  display() draws an icosahedron.
  82.  */
  83. void display(void)
  84. {
  85.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  86.  
  87.     if (!twopass) {
  88.       glEnable(GL_FOG);
  89.     } else {
  90.       glDisable(GL_FOG);
  91.     }
  92.  
  93.     glColor3f (1.0, 1.0, 1.0);
  94.     glutWireIcosahedron();
  95.  
  96.     if (twopass) {
  97.       afDoFinalFogPass(0, 0, width, height);
  98.     }
  99.  
  100.     glutSwapBuffers();
  101. }
  102.  
  103. void myReshape(int w, int h)
  104. {
  105.   width = w;
  106.   height = h;
  107.     glViewport(0, 0, w, h);
  108.     glMatrixMode(GL_PROJECTION);
  109.     glLoadIdentity();
  110.     gluPerspective (45.0, (GLfloat) w/(GLfloat) h, 3.0, 5.0);
  111.     glMatrixMode(GL_MODELVIEW);
  112.     glLoadIdentity ();
  113.     glTranslatef (0.0, 0.0, -4.0);  /*  move object into view   */
  114. }
  115.  
  116. /* ARGSUSED1 */
  117. void
  118. keyboard(unsigned char c, int x, int y)
  119. {
  120.   switch(c) {
  121.   case 'T':
  122.   case 't':
  123.     twopass = 1 - twopass;
  124.   case ' ':
  125.     glutPostRedisplay();
  126.     break;
  127.   }
  128. }
  129.  
  130. void
  131. menu(int value)
  132. {
  133.   switch(value) {
  134.   case 1:
  135.     twopass = 0;
  136.     break;
  137.   case 2:
  138.     twopass = 1;
  139.     break;
  140.   }
  141.   glutPostRedisplay();
  142. }
  143.  
  144. /*  Main Loop
  145.  */
  146. int main(int argc, char** argv)
  147. {
  148.     glutInitWindowSize(380, 380);
  149.     glutInit(&argc, argv);
  150.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  151.     glutCreateWindow("depthcuing by post-rendering pass");
  152.     myinit();
  153.     glutReshapeFunc(myReshape);
  154.     glutDisplayFunc(display);
  155.     glutKeyboardFunc(keyboard);
  156.     glutCreateMenu(menu);
  157.     glutAddMenuEntry("GL_LINEAR depthcueing", 1);
  158.     glutAddMenuEntry("\"add fog\" post-render depthcueing", 2);
  159.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  160.     glutMainLoop();
  161.     return 0;             /* ANSI C requires main to return int. */
  162. }
  163.  
  164.